﻿namespace Web.Control.WWW.Files
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.IO;
    using Lx.Web.Common.ControlsUI;
    using Lx.Web.Common.UI;
    using Model.WWW_WWW.files;
    using BLF.WWW_WWW.Files;
    using System.Collections.Generic;
    using BLF;

    public class Search : BaseControl
    {
        /// <summary>
        /// 排序（1为Most Recent ，2为Most Views ，3为 Most Liked  4为Most Commented）
        /// </summary>
        protected int dt = 1;
        /// <summary>
        /// 页面
        /// </summary>
        protected int pageIndex = 1;
        /// <summary>
        /// 目标语言
        /// </summary>
        protected string Learn = "";
        /// <summary>
        /// 使用语言
        /// </summary>
        protected string BaseL = "";
        /// <summary>
        /// 关键字
        /// </summary>
        protected string key = "";
        /// <summary>
        /// 文件的级别1.Beginner；2.Basic；3.Intermediate；4.None Answers；5.Advanced；可以多选
        /// </summary>
        protected string st = "";
        protected Repeater repFiles;
        protected Repeater repTags;
        protected Repeater rptLL;
        /// <summary>
        /// 数据数量
        /// </summary>
        protected int RecordCount = 0;
        protected List<FileTagModel> tmList;
        protected List<FileDocModel> fdList;
        protected string show = "1";
        protected override void OnLoad(EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["key"]))
            {
                key = Request["key"];
            }
            if (!string.IsNullOrEmpty(Request["st"]) && Request["st"] != "")
            {
                st = Request["st"];
            }
            if (!string.IsNullOrEmpty(Request["sort"]))
            {
                dt = int.Parse(Request["sort"].ToString());
            }
            if (Request.Cookies["italkiLearn"] == null && Request.Cookies["italkiSpeak"] == null)
            {
                HttpCookie llCookie = new HttpCookie("italkiLearn", "all");
                HttpCookie blCookie = new HttpCookie("italkiSpeak", "all");
                Response.Cookies.Add(llCookie);
                Response.Cookies.Add(blCookie);
            }

            if (!string.IsNullOrEmpty(Request["page"]))
            {
                if (Request["page"] == "L")
                {
                    pageIndex = 999;
                }
                else
                {
                    pageIndex = int.Parse(Request["page"].ToString());
                }
            }
            if (!string.IsNullOrEmpty(Request["ll"]) && Request["ll"] != "")
            {
                Learn = Request["ll"];
                HttpCookie llCookie = new HttpCookie("italkiLearn", Learn);
                Response.Cookies.Add(llCookie);
            }
            else
            {
                Learn = Request.Cookies["italkiLearn"].Value;
            }
            if (!string.IsNullOrEmpty(Request["bl"]) && Request["bl"] != "")
            {
                BaseL = Request["bl"];
                HttpCookie blCookie = new HttpCookie("italkiSpeak", BaseL);
                Response.Cookies.Add(blCookie);
            }
            else
            {
                BaseL = Request.Cookies["italkiSpeak"].Value;
            }
            if ((key.Trim() == "" || key.Trim().Length >= 3))
            {

                if (key.Trim() != "" || dt != 1 || BaseL != "" || Learn != "" || st != "")
                {
                    show = "0";


                    fdList = Files.GetFilesDocSearch(Learn, BaseL, dt, pageIndex, 15, key.Replace("?", "").Replace("/", "").Replace("-", "").Replace("&", ""), st, out tmList, out RecordCount);
                    int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)15));
                    if (pageIndex > pCount) pageIndex = pCount;
                    //标题                    
                    this.DynamicTitle = GetText("SF533", new string[] { key });
                    this.DynamicKeywords = GetText("SF534", new string[] { key });
                    this.DynamicDescription = GetText("SF535", new string[] { key });

                    if (repFiles != null)
                    {
                        repFiles.DataSource = fdList;
                        repFiles.DataBind();
                    }
                    if (repTags != null)
                    {
                        repTags.DataSource = tmList;
                        repTags.DataBind();
                    }
                    if (rptLL != null)
                    {
                        List<BLF.LanguageObj> lanObjList = new List<BLF.LanguageObj>();
                        lanObjList.AddRange(BLF.StaticData.GetPopLanguageList);
                        rptLL.DataSource = lanObjList;
                        rptLL.DataBind();
                    }
                    base.OnLoad(e);
                }
            }
            else
            {
                show = "1";
            }
        }



    }
}
